package com.qianli.user.application;

import com.fqgj.common.api.Response;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.facatory.UserBaseInfoFactory;
import com.qianli.user.domain.model.UserBaseInfoModel;
import com.qianli.user.domain.model.base.UserBase;
import com.qianli.user.domain.model.base.UserContact;
import com.qianli.user.domain.model.base.UserResident;
import com.qianli.user.domain.model.base.UserWork;
import com.qianli.user.ro.account.UserAccessRO;
import com.qianli.user.ro.base.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * name:用户基本信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/2 20:16
 * version: v1.0.0
 **/
@Service
public class UserBaseInfoApplication extends UserAbstractApplication {

    @Autowired
    private UserBaseInfoFactory userBaseInfoFactory;

    /**
     * 获取用户基本信息
     *
     * @param userCode
     * @return
     */
    public Response<UserBaseInfoRO> load(String userCode) {
        //加载基本信息模型
        UserBaseInfoModel userBaseInfoModel = new UserBaseInfoModel();
        userBaseInfoModel.setUserCode(userCode);
        userBaseInfoFactory.load(userBaseInfoModel);
        UserBaseInfoRO userBaseInfoRO = convertModelToRO(userBaseInfoModel);
        return Response.ok(userBaseInfoRO);
    }

    private UserBaseInfoRO convertModelToRO(UserBaseInfoModel userBaseInfoModel) {
        String userCode = userBaseInfoModel.getUserCode();
        UserBaseInfoRO userBaseInfoRO = new UserBaseInfoRO();
        userBaseInfoRO.setUserCode(userCode);
        //基础信息
        if (null != userBaseInfoModel.getUserBase()) {
            UserBaseRO userBaseRO = new UserBaseRO();
            userBaseRO.setUserCode(userCode);
            userBaseRO.setAge(userBaseInfoModel.getUserBase().getAge());
            userBaseRO.setAvatar(userBaseInfoModel.getUserBase().getAvatar());
            userBaseRO.setEducation(userBaseInfoModel.getUserBase().getEducation());
            userBaseRO.setGender(userBaseInfoModel.getUserBase().getGender());
            userBaseRO.setMarriedStatus(userBaseInfoModel.getUserBase().getMarriedStatus());
            userBaseRO.setProcreatedStatus(userBaseInfoModel.getUserBase().getProcreatedStatus());
            userBaseRO.setBirthday(userBaseInfoModel.getUserBase().getBirthday());
            //new
            userBaseRO.setFirstName(userBaseInfoModel.getUserBase().getFirstName());
            userBaseRO.setLastName(userBaseInfoModel.getUserBase().getLastName());
            userBaseRO.setFatherName(userBaseInfoModel.getUserBase().getFatherName());
            userBaseRO.setMotherName(userBaseInfoModel.getUserBase().getMotherName());
            userBaseRO.setUsages(userBaseInfoModel.getUserBase().getUsages());

            userBaseInfoRO.setUserBase(userBaseRO);
        }
        //联系方式
        if (null != userBaseInfoModel.getUserContact()) {
            UserContactRO userContact = new UserContactRO();
            userContact.setUserCode(userCode);
            userContact.setWeixin(userBaseInfoModel.getUserContact().getWeixin());
            userContact.setSina(userBaseInfoModel.getUserContact().getSina());
            userContact.setQq(userBaseInfoModel.getUserContact().getQq());
            userContact.setMobile(userBaseInfoModel.getUserContact().getMobile());
            userContact.setEmail(userBaseInfoModel.getUserContact().getMobile());
            userBaseInfoRO.setUserContact(userContact);
        }
        //居住信息
        if (null != userBaseInfoModel.getUserResident()) {
            UserResidentRO userResident = new UserResidentRO();
            userResident.setUserCode(userBaseInfoModel.getUserResident().getUserCode());
            userResident.setAddress(userBaseInfoModel.getUserResident().getAddress());
            userResident.setCity(userBaseInfoModel.getUserResident().getCity());
            userResident.setCityId(userBaseInfoModel.getUserResident().getCityId());
            userResident.setDuration(userBaseInfoModel.getUserResident().getDuration());
            userResident.setProvince(userBaseInfoModel.getUserResident().getProvince());
            userResident.setProvinceId(userBaseInfoModel.getUserResident().getProvinceId());
            userResident.setPinCode(userBaseInfoModel.getUserResident().getPinCode());
            userResident.setPermanentAddress(userBaseInfoModel.getUserResident().getPermanentAddress());
            userResident.setPermanentCity(userBaseInfoModel.getUserResident().getPermanentCity());
            userResident.setPermanentCityId(userBaseInfoModel.getUserResident().getPermanentCityId());
            userResident.setPermanentState(userBaseInfoModel.getUserResident().getPermanentState());
            userResident.setPermanentStateId(userBaseInfoModel.getUserResident().getPermanentStateId());
            userResident.setPermanentPinCode(userBaseInfoModel.getUserResident().getPermanentPinCode());
            userBaseInfoRO.setUserResident(userResident);
        }
        //工作信息
        if (null != userBaseInfoModel.getUserWork()) {
            UserWorkRO userWork = new UserWorkRO();
            userWork.setUserCode(userCode);
            userWork.setAddress(userBaseInfoModel.getUserWork().getAddress());
            userWork.setCareerType(userBaseInfoModel.getUserWork().getCareerType());
            userWork.setPosition(userBaseInfoModel.getUserWork().getPosition());
            userWork.setCity(userBaseInfoModel.getUserWork().getCity());
            userWork.setCityId(userBaseInfoModel.getUserWork().getCityId());
            userWork.setCompanyName(userBaseInfoModel.getUserWork().getCompanyName());
            userWork.setPayDay(userBaseInfoModel.getUserWork().getPayDay());
            userWork.setPhone(userBaseInfoModel.getUserWork().getPhone());
            userWork.setProvince(userBaseInfoModel.getUserWork().getProvince());
            userWork.setProvinceId(userBaseInfoModel.getUserWork().getProvinceId());
            userWork.setSalary(userBaseInfoModel.getUserWork().getSalary());
            userBaseInfoRO.setUserWork(userWork);
        }
        //aadhaar信息
        if (null != userBaseInfoModel.getUserKycDocument()) {
            UserAadhaarRO userAadhaarRO = new UserAadhaarRO();
            BeanUtils.copyProperties(userBaseInfoModel.getUserKycDocument(), userAadhaarRO);
            userBaseInfoRO.setUserAadhaar(userAadhaarRO);
        }

        //userAccesRO
        if (null != userBaseInfoModel.getUserAccess()) {
            UserAccessRO ro = new UserAccessRO();
            BeanUtils.copyProperties(userBaseInfoModel.getUserAccess(), ro);
            ro.setMobile(userBaseInfoModel.getUserAccess().getMobile() != null ? userBaseInfoModel.getUserAccess().getMobile().getMobile() : "");
            userBaseInfoRO.setUser(ro);
        }
        return userBaseInfoRO;
    }

    /**
     * 保存基础信息
     *
     * @param userBase
     * @return
     */
    public Response<Boolean> saveUserBase(UserBaseRO userBase) {
        UserBaseInfoModel oldUm = new UserBaseInfoModel();
        oldUm.setUserCode(userBase.getUserCode());
        userBaseInfoFactory.load(oldUm);

        UserBaseInfoModel userBaseInfoModel = new UserBaseInfoModel();
        userBaseInfoModel.setUserCode(userBase.getUserCode());
        //基础信息
        UserBase ub = new UserBase();
        ub.setUserCode(userBase.getUserCode());
        ub.setAge(userBase.getAge());
        ub.setAvatar(userBase.getAvatar());
        ub.setNickName(userBase.getNickName());
        ub.setBirthday(userBase.getBirthday());
        ub.setGender(userBase.getGender());
        ub.setEducation(userBase.getEducation());
        ub.setMarriedStatus(userBase.getMarriedStatus());
        ub.setProcreatedStatus(userBase.getProcreatedStatus());
        ub.setAvatar(userBase.getAvatar());
        ub.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        if (null != oldUm.getUserBase()) {
            ub.setId(oldUm.getUserBase().getId());
            ub.setItemStatus(UserModelItemStatusEnum.UPDATE.getType());
        }
        userBaseInfoModel.setUserBase(ub);
        //保存模型
        userBaseInfoFactory.store(userBaseInfoModel);
        return Response.ok(true);
    }

    /**
     * 保存联系信息
     *
     * @param userContact
     * @return
     */
    public Response<Boolean> saveUserContact(UserContactRO userContact) {
        UserBaseInfoModel oldUm = new UserBaseInfoModel();
        oldUm.setUserCode(userContact.getUserCode());
        userBaseInfoFactory.load(oldUm);

        UserBaseInfoModel userBaseInfoModel = new UserBaseInfoModel();
        userBaseInfoModel.setUserCode(userContact.getUserCode());
        //联系信息
        UserContact ut = new UserContact();
        ut.setUserCode(userContact.getUserCode());
        ut.setEmail(userContact.getEmail());
        ut.setMobile(userContact.getMobile());
        ut.setQq(userContact.getQq());
        ut.setSina(userContact.getSina());
        ut.setWeixin(userContact.getWeixin());
        ut.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        if (null != oldUm.getUserContact()) {
            ut.setId(oldUm.getUserContact().getId());
            ut.setItemStatus(UserModelItemStatusEnum.UPDATE.getType());
        }
        userBaseInfoModel.setUserContact(ut);
        //保存模型
        userBaseInfoFactory.store(userBaseInfoModel);
        return Response.ok(true);
    }

    /**
     * 保存居住信息
     *
     * @param resident
     * @return
     */
    public Response<Boolean> saveUserResident(UserResidentRO resident) {
        UserBaseInfoModel oldUm = new UserBaseInfoModel();
        oldUm.setUserCode(resident.getUserCode());
        userBaseInfoFactory.load(oldUm);

        UserBaseInfoModel userBaseInfoModel = new UserBaseInfoModel();
        userBaseInfoModel.setUserCode(resident.getUserCode());
        //居住信息
        UserResident ur = new UserResident();
        ur.setUserCode(resident.getUserCode());
        ur.setAddress(resident.getAddress());
        ur.setProvince(resident.getProvince());
        ur.setProvinceId(resident.getProvinceId());
        ur.setCity(resident.getCity());
        ur.setCityId(resident.getCityId());
        ur.setDuration(resident.getDuration());
        ur.setPinCode(resident.getPinCode());

        ur.setPermanentStateId(resident.getPermanentStateId());
        ur.setPermanentState(resident.getPermanentState());
        ur.setPermanentCityId(resident.getPermanentCityId());
        ur.setPermanentCity(resident.getPermanentCity());
        ur.setPermanentAddress(resident.getPermanentAddress());
        ur.setPermanentPinCode(resident.getPermanentPinCode());

        ur.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        if (null != oldUm.getUserResident()) {
            ur.setId(oldUm.getUserResident().getId());
            ur.setItemStatus(UserModelItemStatusEnum.UPDATE.getType());
        }
        userBaseInfoModel.setUserResident(ur);
        //保存模型
        userBaseInfoFactory.store(userBaseInfoModel);
        return Response.ok(true);
    }

    /**
     * 保存工作信息
     *
     * @param userWork
     * @return
     */
    public Response<Boolean> saveUserWork(UserWorkRO userWork) {
        UserBaseInfoModel oldUm = new UserBaseInfoModel();
        oldUm.setUserCode(userWork.getUserCode());
        userBaseInfoFactory.load(oldUm);

        UserBaseInfoModel userBaseInfoModel = new UserBaseInfoModel();
        userBaseInfoModel.setUserCode(userWork.getUserCode());
        //工作信息
        UserWork uw = new UserWork();
        uw.setUserCode(userWork.getUserCode());
        uw.setAddress(userWork.getAddress());
        uw.setCareerType(userWork.getCareerType());
        uw.setPosition(userWork.getPosition());
        uw.setCity(userWork.getCity());
        uw.setCityId(userWork.getCityId());
        uw.setCompanyName(userWork.getCompanyName());
        uw.setPayDay(userWork.getPayDay());
        uw.setProvince(userWork.getProvince());
        uw.setProvinceId(userWork.getProvinceId());
        uw.setPhone(userWork.getPhone());
        uw.setSalary(userWork.getSalary());
        uw.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        if (null != oldUm.getUserWork()) {
            uw.setId(oldUm.getUserWork().getId());
            uw.setItemStatus(UserModelItemStatusEnum.UPDATE.getType());
        }
        userBaseInfoModel.setUserWork(uw);
        //保存模型
        userBaseInfoFactory.store(userBaseInfoModel);
        return Response.ok(true);
    }


}